package com.awo.facedetect.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

/**
 * 人脸框View组件,实现了根据faceDrawInfo中的信息更新人脸框位置
 */
public class FaceDetectInfoView extends View {

    private FaceDrawInfo faceDrawInfo;

    private Paint paint;

    private Matrix matrix;

    private int paintColor = Color.RED;

    // 默认人脸框厚度
    private static final int DEFAULT_FACE_RECT_THICKNESS = 2;

    public FaceDetectInfoView(Context context) { this(context, null); }

    public FaceDetectInfoView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
    }

    public FaceDetectInfoView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
    }

    public FaceDetectInfoView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        paint = new Paint();
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (faceDrawInfo != null &&  canvas != null) {

//            paint.setStyle(Paint.Style.STROKE);
//            paint.setStrokeWidth(DEFAULT_FACE_RECT_THICKNESS);
//            paint.setColor(paintColor);
//            paint.setAntiAlias(true);
//
            int left = faceDrawInfo.getFaceLeftTopX();
            int top = faceDrawInfo.getFaceLeftTopy();
            int right = left + faceDrawInfo.getFaceWidth();
            int bottom = top + faceDrawInfo.getFaceHeight();
//
//            canvas.concat(matrix);

            paint.setStrokeWidth(1);
            paint.setStyle(Paint.Style.FILL_AND_STROKE);
            paint.setTextSize((float) 20);

            String str = "检测到人脸，正在识别请稍后...";
//            canvas.drawText(str, left + 100, bottom - 100, paint);

            @SuppressLint("DrawAllocation") Rect rect = new Rect(((int) (getWidth() * 0.05)), bottom - 200, ((int) (getWidth() * 0.95)), bottom - 50);

            Paint.FontMetricsInt fontMetricsInt = paint.getFontMetricsInt();

            int baseline = rect.centerY() - (fontMetricsInt.top + fontMetricsInt.bottom) / 2;

            paint.setColor(Color.argb(100, 0, 0, 0));
            canvas.drawRect(rect, paint);
            paint.setColor(Color.argb(255, 255, 255, 255));
            paint.setTextAlign(Paint.Align.CENTER);
            canvas.drawText(str, rect.centerX(), baseline, paint);



//            @SuppressLint("DrawAllocation") Path mPath = new Path();
            // 左上
//            Rect rect = faceDrawInfo.getFaceRect();
//            mPath.moveTo(rect.left, rect.top + rect.height() / 4);
//            mPath.lineTo(rect.left, rect.top);
//            mPath.lineTo(rect.left + rect.width() / 4, rect.top);
//            // 右上
//            mPath.moveTo(rect.right - rect.width() / 4, rect.top);
//            mPath.lineTo(rect.right, rect.top);
//            mPath.lineTo(rect.right, rect.top + rect.height() / 4);
//            // 右下
//            mPath.moveTo(rect.right, rect.bottom - rect.height() / 4);
//            mPath.lineTo(rect.right, rect.bottom);
//            mPath.lineTo(rect.right - rect.width() / 4, rect.bottom);
//            // 左下
//            mPath.moveTo(rect.left + rect.width() / 4, rect.bottom);
//            mPath.lineTo(rect.left, rect.bottom);
//            mPath.lineTo(rect.left, rect.bottom - rect.height() / 4);
//            canvas.drawPath(mPath, paint);

        }
    }

    public void updateFaceDrawInfo(FaceDrawInfo faceDrawInfo) {
        this.faceDrawInfo = faceDrawInfo;
        postInvalidate();
    }

    public void updateFaceDrawInfo(FaceDrawInfo faceDrawInfo, Matrix matrix) {
        this.faceDrawInfo = faceDrawInfo;
        this.matrix = matrix;
        postInvalidate();
    }

    public void setPaint(Paint paint) {
        this.paint = paint;
    }

    public void setPaintColor(int paintColor) {
        this.paintColor = paintColor;
    }

    public void update() {
        postInvalidate();
    }
}

